$(function(){
	$("input[type='text']").attr('maxlength','20');
	//修改时加载清管工具
	if($("#flag").val() == "true"){
		$("#showCleanTool").css({display:'block'});
		$("#showInputCleanTool").css({display:'none'});
	}else{
		$("#showCleanTool").css({display:'none'});
		$("#showInputCleanTool").css({display:'block'});
		$("#showInputCleanTool").children("input").val($("#tool").val());
	}
	
	//全选或全不选
	 $("span[name=checkAll]").live("click",function(){
	        var booleanParentSelect=$("span[name=checkAll]").children().hasClass("active");
	        if(!booleanParentSelect){
	        	$("span[name=checkSingle]").children().removeClass("active");
	        }else{
	        	$("span[name=checkSingle]").children().addClass("active");
	        }
	    });
	 //点击子项的选择
	 $("span[name=checkSingle]").live("click",function(){
		 if($("span[name=checkSingle]").children().length > 0){
				var count = 0;
				for (var int = 0; int < $("span[name=checkSingle]").children().length; int++) {
					if($("span[name=checkSingle]").children().eq(int).hasClass("active")){
						count ++;
					}
				}
				if($("span[name=checkSingle]").children().length == count){
					$("span[name=checkAll]").children().addClass("active");
				}else{
					$("span[name=checkAll]").children().removeClass("active");
				}
			}
	 });
	
	//点击修改时checkbox的加载
	if($("span[name=checkSingle]").children().length > 0){
		var count = 0;
		for (var int = 0; int < $("span[name=checkSingle]").children().length; int++) {
			if($("span[name=checkSingle]").children().eq(int).hasClass("active")){
				count ++;
			}
		}
		if($("span[name=checkSingle]").children().length == count){
			$("span[name=checkAll]").children().addClass("active");
		}
	}
	
	 /* 日期格式化 */
	Date.prototype.Format = function(fmt) {
        var o = {
            "M+" : this.getMonth() + 1, //月份
            "d+" : this.getDate(), //日
            "h+" : this.getHours(), //小时
            "m+" : this.getMinutes(), //分
            "s+" : this.getSeconds(), //秒
            "q+" : Math.floor((this.getMonth() + 3) / 3), //季度
            "S" : this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
                    .substr(4 - RegExp.$1.length));
        for ( var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1,
                        (RegExp.$1.length == 1) ? (o[k])
                                : (("00" + o[k])
                                .substr(("" + o[k]).length)));
        return fmt;
    }
	
});

/**
 * 点击GIS地图按钮 加载地图信息
 */
function showGisMap(){
	var gisResourcePath = $("#gisResourcePath").val();
	var resourcePath = $("#resourcePath").val();
	var lineId =$pipeLine.val();
	var pagePath = path;
	//获取收发球桶ID
	var fp = $("#fp").val();
	var tp = $("#tp").val();
	//获取选中的跟踪点ID
	var cpArr = new Array();

	//加载选中的跟踪点地图信息
	var cpGisArr = new Array();
	for (var int2 = 0; int2 < $("span[name=checkSingle]").children().length; int2++) {
		var id = $("span[name=checkSingle]").eq(int2).attr("id");
		if($("span[name=checkSingle]").children().eq(int2).hasClass("active")){
			loadPointOnMap(cpArr,cpGisArr,id);
		}
	}

	//加载收发球筒信息
	loadPiggingInfo(cpGisArr);
	//加载跟踪点导航条信息
	loadPointInfo(cpArr,cpGisArr,resourcePath);
	//跳转地图
	updateGisMap("init");


//	getForm(gisResourcePath + "bMapTrack.jsp",cpGisArr);
}

/**
 * 加载在地图上要显示的跟踪点
 * @param cpArr
 * @param cpGisArr
 */
function loadPointOnMap(cpArr,cpGisArr,id){
	cpArr.push(id);
	var gisObj = $("#"+id).parents("ul");
	//var lng = gisObj.find("input[name='lng']").val();
	//var lat = gisObj.find("input[name='lat']").val();
	//var gisId = gisObj.find("input[name='gisId']").val();
	var customnumber = gisObj.find("input[name='customNumber']").val();
	var code = checkObj(gisObj.find("input[name='code']").val());
	var type = checkObj(gisObj.find("li").eq(1).html());
	var reference = checkObj(gisObj.find("li").eq(4).html());
	var remark = checkObj(gisObj.find("li").eq(6).html());
	var lineName = $("#lineDiv").children("a").html();
	var segmentName = $("#segmentDiv").children("a").html();
	var regionName = gisObj.find("input[name='regionName']").val();
	var recorder = checkObj(gisObj.find("input[name='recorder']").val());
	var passTime = checkDate(gisObj.find("input[name='recordtime']").val());
	var state = "N";
	var gisId = checkObj($("#"+id).next().val());
	var jsonObj = {
		//"lng" : lng,
		//"lat" : lat,
		//"gisId" :gisId,
		"customnumber" : customnumber,
		"code" : code,
		"type" : type,
		"reference" : reference,
		"remark" : remark,
		"lineName" : lineName,
		"segmentName" : segmentName,
		"regionName" : regionName,
		"state" : state,
		"recorder":recorder,
		"recorderTime":passTime,
		"gisId" : gisId
	};
	cpGisArr.push(jsonObj);

}

/**
 * 加载收发球筒信息
 * @param obj
 */
function loadPiggingInfo(obj){
	var frompiginfo = checkObj($("#frompiginfo").val());
	var topiginfo = checkObj($("#topiginfo").val());
	//管道名称
	var lineName = $("#lineDiv").children().html();
	//管段名称
	var segmentName = $("#segmentDiv").children().html();

	var frompiginfoArr = frompiginfo.split("#%#");
	var fromPigJson = {
			'lineName':lineName,
			'segmentName':segmentName,
			////发球筒X坐标
			//'lng' : checkObj(frompiginfoArr[0]),
			////发球筒Y坐标
			//'lat' : checkObj(frompiginfoArr[1]),
			//发球筒gisID
			'gisId' : checkObj(frompiginfoArr[0]),
			//发球筒编号
			'customnumber': checkObj($("#fpcode").val()),
			//发球筒统一编号
			'code' : checkObj(frompiginfoArr[14]),
			//发球筒所属场站阀室
			'regionName' : checkObj(frompiginfoArr[2]),
			//发球筒压力等级
			'press' : checkObj(frompiginfoArr[3]),
			//发球筒的小球桶长度
			'SBlength' : checkObj(frompiginfoArr[4]),
			//发球筒的小球筒直径
			'SBdiameter' : checkObj(frompiginfoArr[5]),
			//发球筒的小球筒壁厚
			'SBthick' : checkObj(frompiginfoArr[6]),
			//发球筒的大球桶长度
			'BBlength' : checkObj(frompiginfoArr[7]),
			//发球筒的大球筒直径
			'BBdiameter' : checkObj(frompiginfoArr[8]),
			//发球筒的大球桶壁厚
			'BBthick' : checkObj(frompiginfoArr[9]),
			//发球筒的生产厂商
			'factory' : checkObj(frompiginfoArr[1]),
			//发球筒的生产日期
			'factoryTime' : frompiginfoArr[10],
			//发球筒的记录人
			'recorder' : checkObj(frompiginfoArr[11]),
			//发球筒的记录时间
			'recordTime' : frompiginfoArr[12],
			//发球筒备注
			'remark':checkObj(frompiginfoArr[13]),

			'pointType' : 'fromGisPig'
	}

	var topiginfoArr = topiginfo.split("#%#");
	var toPigJson = {
			'lineName':lineName,
			'segmentName':segmentName,
			////收球筒X坐标
			//'lng' : checkObj(topiginfoArr[0]),
			////收球筒Y坐标
			//'lat' : checkObj(topiginfoArr[1]),
			//收球筒gisID
			'gisId' : checkObj(topiginfoArr[0]),
			//收球筒编号
			'customnumber': checkObj($("#tpcode").val()),
			//收球筒统一编号
			'code' : checkObj(topiginfoArr[14]),
			//收球筒所属场站阀室
			'regionName' : checkObj(topiginfoArr[2]),
			//收球筒压力等级
			'press' : checkObj(topiginfoArr[3]),
			//收球筒的小球桶长度
			'SBlength' : checkObj(topiginfoArr[4]),
			//收球筒的小球筒直径
			'SBdiameter' : checkObj(topiginfoArr[5]),
			//收球筒的小球筒壁厚
			'SBthick' : checkObj(topiginfoArr[6]),
			//收球筒的大球桶长度
			'BBlength' : checkObj(topiginfoArr[7]),
			//收球筒的大球筒直径
			'BBdiameter' : checkObj(topiginfoArr[8]),
			//收球筒的大球桶壁厚
			'BBthick' : checkObj(topiginfoArr[9]),
			//收球筒的生产厂商
			'factory' : checkObj(topiginfoArr[1]),
			//收球筒的生产日期
			'factoryTime' : topiginfoArr[10],
			//收球筒的记录人
			'recorder' : checkObj(topiginfoArr[11]),
			//收球筒的记录时间
			'recordTime' : topiginfoArr[12],
			//收球筒备注
			'remark':checkObj(topiginfoArr[13]),

			'pointType' : 'toGisPig'
	}

	//发球筒：'pointType':fromGisPig  收球筒 'pointType':toGisPig
	obj.push(fromPigJson);
	obj.push(toPigJson);
}

/**
 * 加载跟踪点在地图下方的导航信息
 * @param cpArr
 * @param cpGisArr
 * @param resourcePath
 */
function loadPointInfo(cpArr,cpGisArr,resourcePath){
	var f = cpGisArr[cpGisArr.length-2];
	var t = cpGisArr[cpGisArr.length-1];
	var _fvalueRoom = $("#fromPig").text();
	var _tvalueRoom = $("#toPig").text();

	$("#showPoint").html("");
	var html = "";
	html += "<li class='start'>" +
	"<i onclick='showPointOnMap(\""+ f.gisId+"\",\"piggingstructure\");'><img src='"+resourcePath+"/images/pig/fa.png' alt=''/></i>" +
	"<span class='s01' title='"+_fvalueRoom+"'>"+_fvalueRoom.substring(0,10)+"</span>" +
	"<span class='s02'>"+"距离:"+$("#fpmile").val()+"</span>" +
	"</li>";
	if(cpArr.length > 0 ){
		$.each(cpArr,function(i,e){
			var id = cpArr[i];
			//var gisObj = $("#"+id).parents("ul");
			//var lng = gisObj.find("input[name='lng']").val();
			//var lat = gisObj.find("input[name='lat']").val();
			var customNumber = $("#"+id).parent().parent().parent().find("input[name='regionName']").val();
			var mile = "距离:"+$("#"+id).parent().parent().parent().find("li").eq(3).html()+" m";
			var gisId = $("#"+id).next().val();
			html +=  "<li class='zhou w80'></li>" +
					"<li class='sl'>" +
					"<i onclick='showPointOnMap(\""+ gisId+"\",\"mark\");'><em class='e01'></em></i>" +
					"<span class='s01-1' title='"+customNumber+"'>"+customNumber.substring(0,10)+"</span>" +
					"<span class='s02-2' style='width:145px;'>"+mile+"</span>" +
					"</li>" ;
		});
	}
	html += "<li class='zhou w80'></li>";
	html += "<li class='start'>" +
	"<i onclick='showPointOnMap(\""+ t.gisId+"\",\"piggingstructure\");'><img src='"+resourcePath+"/images/pig/shou.png' alt=''style='margin-top:4px;margin-left:4px' /></i>" +
	"<span class='s01' title='"+_tvalueRoom+"'>"+_tvalueRoom.substring(0,10)+"</span>" +
	"<span style='width:145px;' class='s02'>"+"距离:"+$("#tpmile").val()+"</span>" +
	"</li>";
	$("#showPoint").html(html);

	swiperWidth($("#aa .sl").length+2);
}


/**
 * 获取清管工具
 * @param name
 */
function getCleanTool(name){
	var pagePath = path;
	if("" != name && null != name && undefined != name){
		var url = pagePath + "/pig/cleanPipeLineManager/getCleanTool.json";
		$().invoke(url,{"name":name},[function(m,r){
			$("#cleanToolUl").html("");
			if(r.mark){
				var clt = r.cleanTool;
				if(null != clt && undefined != clt){
					var html = "";
					$.each(clt,function(i,evalue){
						html += "<li><a href='javascript:;' id='"+evalue.ID+"' title='"+evalue.NAME+"'>"+evalue.NAME+"</a></li>";
					});
					$("#cleanToolUl").html(html);
					$("#showInputCleanTool").css({display:'none'});
//					$("#showInputCleanTool").next().css({'margin-left':'20px'});
					$("#showCleanTool").css({display:'block'});
				}
			}else{
				$("#showCleanTool").css({display:'none'});
				$("#showInputCleanTool").css({display:'block'});
//				$("#showInputCleanTool").next().css({'margin-left':'20px'});
			}
		}]);
	}
}


//获取管道对应的收发球
/*function getPig(lineId){
	$("#segment").html('');
	var pagePath =path;
	var url = pagePath + "/pig/cleanPipeLineManager/getPig.json";
	if(lineId != null){
		$().invoke(url,{"lineId":lineId},[function(m,r){
			var segment = r.segment;
			var fromPig = r.ball.begin;
			var toPig = r.ball.end;
			var html='';
			$.each(segment,function(i,evalue){
				html += "<li onclick='getMarkPoint(\""+evalue.ID+"\")'><a href='javascript:void(0);' id='"+ evalue.ID+"'>"+ evalue.NAME+"</a></li>";
			});
			$("#segmentDiv").html("<a href='javascript:void(0);' id='jcgdmc'>管段名称</a><span></span>");
			$("#segment").html(html);
			//重置收发球
			$("#fromPig").html("场站阀室");
			$("#toPig").html("场站阀室");
			//重置跟踪点
			$("#markPointDiv").html("");
			//重置收发球筒信息
			$("#frompiginfo").val("");
			$("#topiginfo").val("");
			$("span[name=checkAll]").addClass("selected");
			$("span[name=checkAll]").children().removeClass("active");
		}]);
	}
}*/
//根据收发球ID得到收发球

//
/**
 *根据管段ID得到收发球以及mark点
 * @param fromValId
 * @param toValId
 */
function getMarkPoint(pipeLineId,fromValId,toValId,taskId){
	var pagePath = path;
	var hangClass = '';
	if((pipeLineId != "" && fromValId != "" && toValId != "") || taskId != ""){
		var url = pagePath + "/pig/cleanPipeLineManager/getMarkPoint.json";
		$().invoke(url,{"pipeLineId":pipeLineId,"fromValId":fromValId,"toValId":toValId,"taskId":taskId},[function(m,r){
			var html = "";
			if( r.markPoint != null &&　r.markPoint != ""){
				var p = r.markPoint;
			}
			if(r.pigging != null && r.pigging.fromValRoom != null &&　"" != r.pigging.fromValRoom){
				var pig = r.pigging;
			}
			if(null != p && undefined != p){
				$.each(p,function(i,evalue){
					hangClass = 'ul-header bg-white';
					html += "<div sort='"+i+"' class='info-tr'>" +
							"<ul class='"+hangClass+"'>" +
								"<li class='new-w5'>" +
								"<span class='table-ck'><span name='checkSingle' class='check-box selected' id="+evalue.ID+" onclick=''><i></i></span><input type='hidden' value='"+evalue.GISID+"'/></span>" +
								"</li>" +
								"<li class='new-w10'>"+checkObj(evalue.TYPE)+"</li>" +
								"<li class='new-w10'>"+checkObj(evalue.REGIONNAME)+"</li>" +
								"<li class='new-15'>"+checkObj(evalue.MILEAGE)+"</li>" +
								"<li class='new-15'>"+checkObj(evalue.REFERENCE)+"</li>" +
								"<li class='new-15'>"+checkObj(evalue.DISTANCE)+"</li>" +
								"<li class='new-15'>"+checkObj(evalue.REMARK)+"</li>" +
								"<li class='new-15 no-right'>" +
						             "<a class='d-gis' onclick='showSinglePointOnMap(this)' ></a></li>" +
									//"<input type='hidden' name='lng' value='"+checkObj(evalue.LNG)+"'/>" +
									//"<input type='hidden' name='lat' value='"+checkObj(evalue.LAT)+"' />" +
						            "<input type='hidden' name= 'gisId'value='"+evalue.GISID+"'/>" +
									"<input type='hidden' name='code' value='"+checkObj(evalue.CODE)+"'/>" +
									"<input type='hidden' name='name' value='"+checkObj(evalue.NAME)+"' />" +
									"<input type='hidden' name='regionName' value='"+checkObj(evalue.REGIONNAME)+"'/>" +
									"<input type='hidden' name='recorder' value='"+checkObj(evalue.RECORDER)+"'/>" +
									"<input type='hidden' name='recordtime' value='"+checkDate(evalue.RECORDTIME)+"'/>" +
									"<input type='hidden' name='customNumber' value='"+checkObj(evalue.CUSTOMNUMBER)+"'/>" +
							"</div>";
				});
				$("#markPointDiv").html("");
				$("#markPointDiv").html(html);
			}
			if(null != pig && undefined != pig){
				$("#fromPig").html((null == pig.fromValRoom || "" == pig.fromValRoom ? '场站阀室': pig.fromValRoom) + "<input type='hidden' value='"+pig.fromPig+"'/>");
				$("#toPig").html(null == pig.toValRoom || "" == pig.toValRoom ? '场站阀室': pig.toValRoom+ "<input type='hidden' value='"+pig.toPig+"'/>");
				$("#fpmile").val(pig.fromMile);
				$("#tpmile").val(pig.toMile);
				$("#fpcode").val(pig.fromPigStr);
				$("#tpcode").val(pig.toPigIdStr);
				$("#frompiginfo").val(pig.frompiginfo);
				$("#topiginfo").val(pig.topiginfo);
			}
			$("span[name=checkAll]").addClass("selected");
			$("span[name=checkAll]").children().removeClass("active");
			//更新地图
			if($("#showGis").hasClass("active")){
				//更新地图
				updateGisMap('update');
			}

		}]);
	}
}


/*
 *保存任务
 */
function saveTask(){
	var pagePath =path;
	var url = pagePath + "/pig/cleanPipeLineManager/saveTask.json";

	var taskId = $("#taskId").val();
	var taskName = $.trim($("#taskName").val());
	var lineId = $pipeLine.val();
	var remark = $.trim($("#remark").val());
	var cleanTypeId = $("#cleanTypeDiv").children("a").attr("id");
	var cleanTypeName = $("#cleanTypeDiv").children("a").html();
	var cType = "漏磁检测,超声波检测,几何检测";
	var cleanTool = "";
	var fromValRoomId = $begin.val();
	var toValRoomId =$end.val();
	var fromId = $("#fromPig").children().val();
	var toId = $("#toPig").children().val();
	var planPagDateTime = $("#planPagDateTime").val();
	var reverse = $("#reverse").val();
	//mark点
	var checkFlag = false;
	var checkSingleBox = $("span[name='checkSingle']");
	var markPointArr = new Array();
	var markPoint = "";

	if("" == taskName){
		$(this).MSG({content:'请输入任务名称'});
		return ;
	}
	if(null == lineId || "" == lineId || undefined == lineId){
		$(this).MSG({content:'请选择管道'});
		return;
	}
	if(null== fromValRoomId || "" == fromValRoomId || undefined == fromValRoomId){
		$(this).MSG({content:'请选择起点位置'});
		return ;
	}
	if(null== toValRoomId || "" == toValRoomId || undefined == toValRoomId){
		$(this).MSG({content:'请选择终点位置'});
		return ;
	}
	if("清管类型" == cleanTypeName){
		$(this).MSG({content:'请选择清管类型'});
		return ;
	}
	if(cType.indexOf(cleanTypeName) >= 0){
		cleanTool = $("#cleanToolDiv").children("a").attr("id");
	}else{
		cleanTool = $.trim($("#showInputCleanTool").children("input").val());
	}
//	if(cleanTool == "" || cleanTool == "清管工具"){
	if("" == checkObj(cleanTool)){
		$(this).MSG({content:'请选择或填写清管工具'});
		return ;
	}
	if(planPagDateTime == "" || planPagDateTime == null){
		$(this).MSG({content:'计划清管时间不能为空'});
		return ;
	}
	if("" == fromId || "" == toId ){
		$(this).MSG({content:'收发球筒所属阀室不能为空，请重新选择起止点位置'});
		return ;
	}
	if(checkSingleBox.length < 1){
		$(this).MSG({content:'该管段没有对应的跟踪点'});
		return;
	}else{
		for(var i= 0 ; i < checkSingleBox.length;i ++){
			if(checkSingleBox[i].innerHTML.indexOf("active") > 0){
				markPointArr.push(checkSingleBox[i].id);
				checkFlag = true;
			}
		}
		markPoint = markPointArr.join();
		if(markPoint.length < 1){
			$(this).MSG({content:'请先勾选跟踪点'});
			return ;
		}
	}

	if(checkFlag){
		$("#form1").find("input[name='taskId']").val(taskId);
		$("#form1").find("input[name='taskName']").val(taskName);
		$("#form1").find("input[name='lineId']").val(lineId);
		$("#form1").find("input[name='fromValRoomId']").val(fromValRoomId);
		$("#form1").find("input[name='toValRoomId']").val(toValRoomId);
		$("#form1").find("input[name='markPoint']").val(markPoint);
		$("#form1").find("input[name='fromPigStr']").val(fromId);
		$("#form1").find("input[name='toPigIdStr']").val(toId);
		$("#form1").find("input[name='remark']").val(remark);
		$("#form1").find("input[name='planPagDateTime']").val(planPagDateTime);
		$("#form1").find("input[name='pigType']").val(cleanTool);
		$("#form1").find("input[name='type']").val(cleanTypeId);
		$("#form1").find("input[name='isAdverseCurrent']").val(reverse);
		$("#form1").ajaxSubmit({
			url:url,
			dataType:'json',
			success:function(r){
				var msgCode = r.result.msgCode;
				if(undefined == msgCode){
					$(this).MSG({
						type: 'error',
						content: '操作失败，请刷新页面重试！',
					});
				}else if(msgCode == 10){
					$(this).MSG({
						type: 'error',
						content: '任务名称已经被使用，请重新输入！',
					});
				}else if(msgCode == "done"){
					$(this).MSG({
						type: 'success',
						content: "操作成功！",
					});
					window.location.href = pagePath + "/pig/cleanPipeLineManager/getCleanPipeLineManagerIndex.html";
				}
			}
		});
	}
}


function checkObj(obj){
	if("" == obj || null == obj || undefined == obj){
		return "";
	}else{
		return obj;
	}
}

function checkDate(obj){
    return null==obj || "" == $.trim(obj) || undefined == obj ? "" : new Date(obj).Format("yyyy-MM-dd hh:mm:ss");
}

function showPointOnMap(gisId,typeElement){
	document.getElementById("dyMap").contentWindow.locateTarget(gisId,typeElement);
}


/**
 * 加载地图调用的方法
 * @param url 地图绝对路径
 * @param ap
 */
function getForm(url,ap,room,pipeinfo,lng,lat){
	var formObj = document.createElement("form");
	formObj.setAttribute("method","post");
	formObj.setAttribute("action",url);
	var inputObj = document.createElement("input");
	var inputObj2 = document.createElement("input");
	var inputObj3 = document.createElement("input");

	var inputObj4 = document.createElement("input");
	var inputObj5 = document.createElement("input");

	inputObj.setAttribute("type","hidden");
	inputObj.setAttribute("name","gisIdArr");
	inputObj2.setAttribute("type","hidden");
	inputObj2.setAttribute("name","valveRoomAll");
	inputObj3.setAttribute("type","hidden");
	inputObj3.setAttribute("name","pipeInfo");

	inputObj4.setAttribute("type","hidden");
	inputObj4.setAttribute("name","lng");

	inputObj5.setAttribute("type","hidden");
	inputObj5.setAttribute("name","lat");

	var valueStr = "";
	var valueStr2 = "";
	var valueStr3 = "";
	valueStr = JSON.stringify(ap);
//	valueStr2 =JSON.stringify(room);
	inputObj.setAttribute("value",valueStr);
	inputObj2.setAttribute("value",room);
	inputObj3.setAttribute("value",pipeinfo);

	inputObj4.setAttribute("value",lng);
	inputObj5.setAttribute("value",lat);

	formObj.appendChild(inputObj);
	formObj.appendChild(inputObj2);
	formObj.appendChild(inputObj3);

	formObj.appendChild(inputObj4);
	formObj.appendChild(inputObj5);
	document.getElementById('dyMap').contentWindow.document.write(formObj.outerHTML);
	document.getElementById('dyMap').contentWindow.document.getElementsByTagName("form")[0].submit();
}

/**
 * 列表点击一条跟踪点 在地图上显示
 * @param obj
 */
function showSinglePointOnMap(obj){
	/*var gisResourcePath = $("#gisResourcePath").val();
	var resourcePath = $("#resourcePath").val();
	var pagePath = path;
	var segmentId = $("#segmentDiv").children("a").attr("id");
	//获取收发球桶ID
	var fp = $("#fp").val();
	var tp = $("#tp").val();
	//获取选中的跟踪点ID
	var cpArr = new Array();
	//加载选中的跟踪点地图信息
	var cpGisArr = new Array();

	//先移除所有checkbox的选中事件
	//$("#markPointDiv").find("span[name='checkSingle']").children().removeClass("active");
	//为本条数据添加选中
	//var pointObj = $(obj).parent().find("span[name='checkSingle']").children().addClass("active");

	$("#showList").removeClass("active");
	$("#showGis").addClass("active");
	$(".fu").children().eq(0).addClass("none");
	$(".fu").children().eq(1).removeClass("none");

	var checkboxObj = $("span[name=checkSingle]").children();
	for (var i = 0; i < checkboxObj.length; i++) {
		var id = $("span[name=checkSingle]").eq(i).attr("id");
		loadPointOnMap(cpArr,cpGisArr,id);
	}
	//加载收发球筒信息
	loadPiggingInfo(cpGisArr);
	//加载跟踪点导航条信息
	loadPointInfo(cpArr,cpGisArr,resourcePath);

	var lng = $(obj).parent().next().val();
	var lat = $(obj).parent().next().next().val();

	var url = pagePath + "/pig/cleanPipeLineManager/findAllValueRoom.json";
	$().invoke(url,{"segmentId":segmentId},[function(m,r){
		getForm(gisResourcePath + "bMapTrackLoction.jsp",cpGisArr,r.data,r.data2,lng,lat);
	}]);*/
	var resourcePath = $("#resourcePath").val();
    var gisResourcePath = $("#gisResourcePath").val();
	var lineId =  $pipeLine.val();
	var fromValRoomId =  $begin.val();
	var toValRoomId =$end.val();
	var reverse = $("#reverse").val();

	var url = path + "/pig/cleanPipeLineManager/findAllMapData.json";
	resultAjax(url,{"lineId":lineId,"fromValId":fromValRoomId,"toValId":toValRoomId,"reverse":reverse},function(data) {
		if(data.result != undefined)
		{
			data=data.result;
		}

		data[3].gisIdArray = "";
		var checkboxObj = $("span[name=checkSingle]").children()
		var gisArr = new Array();
		var markArr = new Array();
		var cpGisArr = new Array();
		for (var i = 0; i < checkboxObj.length; i++) {
			if (checkboxObj.eq(i).hasClass("active")) {
				var id = $("span[name=checkSingle]").eq(i).attr("id");
				loadPointOnMap(markArr,cpGisArr,id);
				var gisId = checkObj($("#" + id).next().val());
				gisArr.push(gisId);
			}

		}
		data[3].gisIdArray = gisArr;
		var markGisId =$(obj).parent() .next().val();
		var jsonObj={
			"typeName":"loc",
			"gisId":markGisId,
			"typeElement":'mark'
		}
		data.push(jsonObj);

		$("#showList").removeClass("active");
		$("#showGis").addClass("active");
		$(".fu").children().eq(0).addClass("none");
		$(".fu").children().eq(1).removeClass("none");
		//更新收发球筒信息
		var resourcePath = $("#resourcePath").val();
		loadPiggingInfo(cpGisArr);
		//更新地图下方的导航条的收发球
		loadPointInfo(markArr,cpGisArr,resourcePath);
		loadGis($("#mapDiv"), gisResourcePath + "baseMapPigTaskManageLoc.jsp",data);
	},"json");
}

/**
 * ajx 方法
 * @param url 访问地址ַ
 * @param data 访问参数
 * @param m_fun 回调方法
 * @param type 返回数据类型 xml html json txt 等
 */
resultAjax=function(url,data,m_fun,type,result) {
	console.log(">>>>>");
	var conf = {"stringFormat": true};
	$().invoke(url, data, [function (m, r) {
		if (type == 'JSON' || type == 'json') {
			r = jQuery.parseJSON(r);
		}
		m_fun.apply(self, [r, result]);
	}], conf);
	return;
}
//更新地图或跳转地图
function updateGisMap(type){
	var gisResourcePath = $("#gisResourcePath").val();
	var lineId =$pipeLine.val();
	var fromValId = $begin.val();
	var toValId =$end.val();
	var url = path + "/pig/cleanPipeLineManager/findAllMapData.json";

	resultAjax(url,{"lineId":lineId,"fromValId":fromValId,"toValId":toValId,"reverse":$("#reverse").val()},function(data){
		data.result[3].gisIdArray="";
		var checkboxObj = $("span[name=checkSingle]").children()
		var gisArr = new Array();
		var markArr = new Array();
		var cpGisArr = new Array();
		for (var i = 0; i < checkboxObj.length; i++) {
			if(checkboxObj.eq(i).hasClass("active")){
				var id = $("span[name=checkSingle]").eq(i).attr("id");
				//更新跟踪点信息
				loadPointOnMap(markArr,cpGisArr,id)
				var gisId = checkObj($("#"+id).next().val());
				gisArr.push(gisId);
			}

		}
		data.result[3].gisIdArray = gisArr;
		if('init'==type) {
			loadGis($("#mapDiv"), gisResourcePath + "baseMapPigTaskManage.jsp", data.result);
		}else{

			var resourcePath = $("#resourcePath").val();
			//更新收发球信息
			loadPiggingInfo(cpGisArr);
			//更新地图下方的导航条的收发球
			loadPointInfo(markArr,cpGisArr,resourcePath)
			data.result[3].gisIdArray=gisArr;
			document.getElementById("dyMap").contentWindow.showTrackLayer(data.result);

		}

	},"json");


}

/**
 * 收发球筒易位
 */
function transPig(){
	//换收发球筒的id
	var trans = $("#fromPig").html();
	$("#fromPig").html($("#toPig").html());
	$("#toPig").html(trans);

	/*var segmentId = $("#segmentDiv").children().attr("id");
	var adverseCurrent = $("#adverseCurrent").val();
	var url =
	$.invoke(url,data,[function(r){

	}]);*/
	//换跟踪点顺序
	var listObj = $("#markPointDiv").children(".info-tr");
	var arr = new Array();
	for (var i = 0; i < listObj.length; i++) {
		arr.push(listObj.eq(i))
	}
	arr.reverse();
	for(var i=0;i<arr.length;i++)
	{
		$("#markPointDiv").append(arr[i]);
	}
	//换收发球筒的基本信息
	var transInfo = $("#frompiginfo").val();
	$("#frompiginfo").val($("#topiginfo").val());
	$("#topiginfo").val(transInfo);
	//换收发球筒相对距离
	var transMile = $("#fpmile").val();
	$("#fpmile").val($("#tpmile").val());
	$("#tpmile").val(transMile);

	var reverse = $("#reverse").val();
	if(0==reverse){
		$("#reverse").val(1);
	}
	if(1==reverse){
		$("#reverse").val(0);
	}

	if($("#showGis").hasClass("active")){
		//更新地图
		updateGisMap('update');
	}
}

function chooseFile(obj){
	$("#file").trigger('click');
}
//function changeFile(){
//	$(".fj-btn").children("span").html($("#file").val());
//}




//判断文件大小
function changeFile(obj){
  
  var fileSize = 0;
  var isIE = /msie/i.test(navigator.userAgent) && !window.opera;            
  if (isIE && !obj.files) {          
       var filePath = obj.value;            
       var fileSystem = new ActiveXObject("Scripting.FileSystemObject");   
       var file = fileSystem.GetFile (filePath);               
       fileSize = file.Size;         
  }else {  
       fileSize = obj.files[0].size;     
  } 
  fileSize=Math.round(fileSize/1024/1024*100)/100; //单位为KB
  if(fileSize>=5){
//	  $(".fj-btn").children("span").html("");
	  $(obj).val("");
	  $(this).MSG({content:'上传文件不得超过(包含)5兆'});
      return false;
  }else{
	  $(".fj-btn").children("span").css("title",$("#file").val());
	  $(".fj-btn").children("span").html($("#file").val());
	  
  }
}